package com.fd.wpx.authorizating.service.impl;

import com.fd.wpx.authorizating.pojo.PermissionContext;
import com.fd.wpx.authorizating.redis.JedisClient;
import com.fd.wpx.authorizating.session.RemoteSessionService;
import com.fd.wpx.blog.mapper.SysPrivilegeMapper;
import com.fd.wpx.blog.mapper.SysRoleMapper;
import com.fd.wpx.blog.pojo.SysPrivilege;
import com.fd.wpx.blog.pojo.SysPrivilegeExample;
import com.fd.wpx.blog.pojo.SysRole;
import com.fd.wpx.blog.pojo.SysRoleExample;
import com.fd.wpx.common.pojo.BlogResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;


@Service
public class RemoteSessionServiceImpl implements RemoteSessionService {
    @Autowired
    private JedisClient jedisClient;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;
    @Value("${expire}")
    private int expire;
    @Override
    public BlogResult getSession(Serializable sessionId) {
        String strSession=jedisClient.get("session."+sessionId);
        jedisClient.expire("session."+sessionId,expire);
        if(strSession==null)
        {
            return BlogResult.build(404,"session不存在");
        }
        return BlogResult.build(200,"session存在",strSession);
    }
    @Override
    public void saveSession(Serializable sessionId,String session) {
        jedisClient.set("session."+sessionId,session);
        jedisClient.lpush("session.ids",(String)sessionId);
        jedisClient.expire("session."+sessionId,expire);
    }

    @Override
    public void updateSession(Serializable sessionId,String session) {
        System.out.println("updateSession");
        jedisClient.set("session."+sessionId,session);
        jedisClient.expire("session."+sessionId,expire);
    }

    @Override
    public void deleteSession(Serializable sessionId) {
        jedisClient.del("session."+sessionId);
        jedisClient.lrem("session.ids",0,(String)sessionId);
        jedisClient.expire("session."+sessionId,expire);

    }

    @Override
    public PermissionContext getPermissions(String username) {
        System.out.println("getPermissions");
        return null;
    }

    @Override
    public PermissionContext getPermissionsByRole(String role) {
        SysRoleExample example=new SysRoleExample();
        SysRoleExample.Criteria criteria=example.createCriteria();
        criteria.andNameEqualTo(role);
        List<SysRole> roles=sysRoleMapper.selectByExample(example);
        //roles的size一定为0，name是唯一的
        String [] strPids=roles.get(0).getPids().split(",");
        SysPrivilegeExample privilegeExample=new SysPrivilegeExample();
        SysPrivilegeExample.Criteria privilegeCriteria=privilegeExample.createCriteria();
        privilegeCriteria.andIdIn(new ArrayList<Integer>(Arrays.asList(convertInteger(strPids))));
        List<SysPrivilege> privileges = sysPrivilegeMapper.selectByExample(privilegeExample);
        PermissionContext context=new PermissionContext();
        Set<String> set=new HashSet<>();
        for(int i=0;i<privileges.size();i++)
        {
            set.add(privileges.get(i).getName());
        }
        context.setPermissions(set);
        return context;
    }
    private Integer[] convertInteger(String [] strPids)
    {
        Integer[] result=new Integer[strPids.length];
        for(int i=0;i<strPids.length;i++){
            result[i]=Integer.parseInt(strPids[i]);
        }
        return result;

    }
}
